#include"common_headers.h"

int movedir31(char source_dir_path[300],char dest_dir_path[300])
{

    if(strcmp(dest_dir_path,"")==0)
    {
        printf("\nmovedir_FAILURE %s",ERR_VFS_MOVEDIR_04);
        return 0;
    }
    if(strcmp(source_dir_path,"")==0)
    {
        printf("\nmovedir_FAILURE %s",ERR_VFS_MOVEDIR_03);
        return 0;
    }
    if(search_nary(dest_dir_path)==0)
    {
        printf("\nmovedir_FAILURE %s",ERR_VFS_MOVEDIR_02);
        return 0;
    }

    struct narytree *temp,*newadd;
    temp=nroot;
    if(temp==NULL) return 0;
    else
    {

        if(strcmp(source_dir_path,temp->mydes->locationpath)==0)
        {
            printf("\nmovedir_FAILURE %s",ERR_VFS_MOVEDIR_06);
            return 0;
        }
        else
        {


            while(1)
            {


                if(strncmp(source_dir_path,temp->mydes->locationpath,strlen(temp->mydes->locationpath))==0 && (temp==nroot || source_dir_path[strlen(temp->mydes->locationpath)]=='/'))
                {
                    if(temp->child==NULL)
                    {

                        printf("\nmovedir_FAILURE %s",ERR_VFS_MOVEDIR_01);
                        return 0;
                    }

                    else
                    {

                        temp=temp->child;
                    }
                }
                else if(temp->rightsibling==NULL && strcmp(source_dir_path,temp->mydes->locationpath)!=0)
                {

                    printf("\nmovedir_FAILURE %s",ERR_VFS_MOVEDIR_01);
                    return 0;
                }
                else if(temp->rightsibling!=NULL && strcmp(source_dir_path,temp->mydes->locationpath)!=0)
                {

                    temp=temp->rightsibling;
                }



                if(strcmp(source_dir_path,temp->mydes->locationpath)==0)
                {


                    if((temp->parent!=NULL) && (temp->rightsibling==NULL) && (temp->leftsibling!=NULL))
                    {
                        char errorhandling[300];
                        strcpy(errorhandling,dest_dir_path);
                        if(strcmp(errorhandling,"/")!=0)
                        {
                            strcat(errorhandling,"/");
                        }
                        strcat(errorhandling,temp->mydes->filename);
                        int find=search_nary(errorhandling);
                        if(find==1)
                        {
                            printf("\nmovedir_FAILURE %s",ERR_VFS_MOVEDIR_05);
                            return 0;
                        }
                        struct narytree *tempo;
                        tempo=(struct narytree *)malloc(sizeof(struct narytree));
                        //tempo=temp;
                        //header.used_file_descriptors--;
                        //freelist[temp->mydes->locationblocknumber]=0;

                        //free_nary(tempo->child);
                        temp->leftsibling->rightsibling=NULL;
                        tempo->mydes=temp->mydes;
                        tempo->parent=NULL;
                        tempo->rightsibling=NULL;
                        tempo->leftsibling=NULL;

                        if(temp->child!=NULL)
                        {
                            tempo->child=temp->child;
                            //temp->child->parent=NULL;
                            tempo->child->parent=tempo;
                            while(tempo->child->rightsibling!=NULL)
                            {
                                tempo->child->rightsibling->parent=tempo;
                            }
                        }
                        else tempo->child=NULL;
                        delete_bstnode(bst_root,temp->mydes->locationpath);////////////////

                        free(temp);/////////////////////////


                        if(strcmp(dest_dir_path,"/")!=0)
                        {
                            strcat(dest_dir_path,"/");
                        }

                        strcat(dest_dir_path,tempo->mydes->filename);
                        strcpy(tempo->mydes->locationpath,dest_dir_path);
                        int state=move_nary(tempo);///////////////////////////////////////////////
                        if(state==1) printf("\nmovedir_SUCCESS");
                        return 1;
                    }
                    else if((temp->parent!=NULL) && (temp->rightsibling!=NULL) && (temp->leftsibling!=NULL))
                    {
                        char errorhandling[300];
                        strcpy(errorhandling,dest_dir_path);
                        if(strcmp(errorhandling,"/")!=0)
                        {
                            strcat(errorhandling,"/");
                        }
                        strcat(errorhandling,temp->mydes->filename);
                        int find=search_nary(errorhandling);
                        if(find==1)
                        {
                            printf("\nmovedir_FAILURE %s",ERR_VFS_MOVEDIR_05);
                            return 0;
                        }
                        struct narytree *tempo;
                        tempo=(struct narytree *)malloc(sizeof(struct narytree));

                        temp->leftsibling->rightsibling=temp->rightsibling;
                        temp->rightsibling->leftsibling=temp->leftsibling;
                        tempo->mydes=temp->mydes;
                        tempo->parent=NULL;
                        tempo->rightsibling=NULL;
                        tempo->leftsibling=NULL;
                        if(temp->child!=NULL)
                        {
                            //temp->child->parent=NULL;
                            tempo->child=temp->child;
                            tempo->child->parent=tempo;
                            while(tempo->child->rightsibling!=NULL)
                            {
                                tempo->child->rightsibling->parent=tempo;
                            }
                        }
                        else tempo->child=NULL;


                        delete_bstnode(bst_root,temp->mydes->locationpath);
                        free(temp);
                        if(strcmp(dest_dir_path,"/")!=0)
                        {
                            strcat(dest_dir_path,"/");
                        }
                        strcat(dest_dir_path,tempo->mydes->filename);
                        strcpy(tempo->mydes->locationpath,dest_dir_path);
                        int state=move_nary(tempo);
                        if(state==1) printf("\nmovedir_SUCCESS");
                        return 1;
                    }
                    else if((temp->parent!=NULL) && (temp->rightsibling!=NULL) && (temp->leftsibling==NULL))
                    {
                        char errorhandling[300];
                        strcpy(errorhandling,dest_dir_path);
                        if(strcmp(errorhandling,"/")!=0)
                        {
                            strcat(errorhandling,"/");
                        }
                        strcat(errorhandling,temp->mydes->filename);
                        int find=search_nary(errorhandling);
                        if(find==1)
                        {
                            printf("\nmovedir_FAILURE %s",ERR_VFS_MOVEDIR_05);
                            return 0;
                        }
                        struct narytree *tempo;
                        tempo=(struct narytree *)malloc(sizeof(struct narytree));
                        temp->rightsibling->leftsibling=NULL;
                        temp->parent->child=temp->rightsibling;
                        tempo->mydes=temp->mydes;
                        tempo->parent=NULL;
                        tempo->rightsibling=NULL;
                        tempo->leftsibling=NULL;
                        if(temp->child!=NULL)
                        {
                            //temp->child->parent=NULL;
                            tempo->child=temp->child;
                            tempo->child->parent=tempo;
                            while(tempo->child->rightsibling!=NULL)
                            {
                                tempo->child->rightsibling->parent=tempo;
                            }
                        }
                        else tempo->child=NULL;

                        delete_bstnode(bst_root,temp->mydes->locationpath);
                        free(temp);
                        if(strcmp(dest_dir_path,"/")!=0)
                        {
                            strcat(dest_dir_path,"/");
                        }
                        strcat(dest_dir_path,tempo->mydes->filename);
                        strcpy(tempo->mydes->locationpath,dest_dir_path);
                        int state=move_nary(tempo);
                        if(state==1) printf("\nmovedir_SUCCESS");
                        return 1;
                    }
                    else if((temp->parent!=NULL) && (temp->rightsibling==NULL) && (temp->leftsibling==NULL))
                    {
                        char errorhandling[300];
                        strcpy(errorhandling,dest_dir_path);
                        if(strcmp(errorhandling,"/")!=0)
                        {
                            strcat(errorhandling,"/");
                        }
                        strcat(errorhandling,temp->mydes->filename);
                        int find=search_nary(errorhandling);
                        if(find==1)
                        {
                            printf("\nmovedir_FAILURE %s",ERR_VFS_MOVEDIR_05);
                            return 0;
                        }
                        struct narytree *tempo;
                        tempo=(struct narytree *)malloc(sizeof(struct narytree));
                        temp->parent->child=NULL;
                        tempo->mydes=temp->mydes;
                        tempo->parent=NULL;
                        tempo->rightsibling=NULL;
                        tempo->leftsibling=NULL;
                        if(temp->child!=NULL)
                        {
                            //temp->child->parent=NULL;
                            tempo->child=temp->child;
                            tempo->child->parent=tempo;
                            while(tempo->child->rightsibling!=NULL)
                            {
                                tempo->child->rightsibling->parent=tempo;
                            }
                        }
                        else tempo->child=NULL;

                        delete_bstnode(bst_root,temp->mydes->locationpath);
                        free(temp);
                        if(strcmp(dest_dir_path,"/")!=0)
                        {
                            strcat(dest_dir_path,"/");
                        }
                        strcat(dest_dir_path,tempo->mydes->filename);
                        strcpy(tempo->mydes->locationpath,dest_dir_path);
                        int state=move_nary(tempo);
                        if(state==1) printf("\nmovedir_SUCCESS");
                        return 1;
                    }


                }




            }
        }
    }
}

